home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2008 February / PCWFEB08.iso / Software / Freeware / Miro 1.0 / Miro_Installer.exe / Miro_Downloader.exe / dl_daemon / subnetparse.pyc (.txt) < prev   
Encoding:
Python Compiled Bytecode  |  2007-11-12  |  5.7 KB  |  254 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.5)
  3.  
  4. from bisect import bisect, insort
  5.  
  6. try:
  7.     True
  8. except:
  9.     True = 1
  10.     False = 0
  11.  
  12. hexbinmap = {
  13.     '0': '0000',
  14.     '1': '0001',
  15.     '2': '0010',
  16.     '3': '0011',
  17.     '4': '0100',
  18.     '5': '0101',
  19.     '6': '0110',
  20.     '7': '0111',
  21.     '8': '1000',
  22.     '9': '1001',
  23.     'a': '1010',
  24.     'b': '1011',
  25.     'c': '1100',
  26.     'd': '1101',
  27.     'e': '1110',
  28.     'f': '1111',
  29.     'x': '0000' }
  30. chrbinmap = { }
  31. for n in xrange(256):
  32.     b = []
  33.     nn = n
  34.     for i in xrange(8):
  35.         if nn & 128:
  36.             b.append('1')
  37.         else:
  38.             b.append('0')
  39.         nn <<= 1
  40.     
  41.     chrbinmap[n] = ''.join(b)
  42.  
  43.  
  44. def to_bitfield_ipv4(ip):
  45.     b = ''
  46.     for n in ip.split('.'):
  47.         b += chrbinmap[int(n)]
  48.     
  49.     return b
  50.  
  51.  
  52. def to_bitfield_ipv6(ip):
  53.     b = ''
  54.     doublecolon = False
  55.     if ip == '':
  56.         raise ValueError, 'bad address'
  57.     
  58.     if ip == '::':
  59.         ip = ''
  60.     elif ip[:2] == '::':
  61.         ip = ip[1:]
  62.     elif ip[0] == ':':
  63.         raise ValueError, 'bad address'
  64.     elif ip[-2:] == '::':
  65.         ip = ip[:-1]
  66.     elif ip[-1] == ':':
  67.         raise ValueError, 'bad address'
  68.     
  69.     for n in ip.split(':'):
  70.         if n == '':
  71.             if doublecolon:
  72.                 raise ValueError, 'bad address'
  73.             
  74.             doublecolon = True
  75.             b += ':'
  76.             continue
  77.         
  78.         if n.find('.') >= 0:
  79.             n = to_bitfield_ipv4(n)
  80.             b += n + '0' * (32 - len(n))
  81.             continue
  82.         
  83.         n = 'x' * (4 - len(n)) + n
  84.         for i in n:
  85.             b += hexbinmap[i]
  86.         
  87.     
  88.     if doublecolon:
  89.         pos = b.find(':')
  90.         b = b[:pos] + '0' * (129 - len(b)) + b[pos + 1:]
  91.     
  92.     if len(b) != 128:
  93.         raise ValueError, 'bad address'
  94.     
  95.     return b
  96.  
  97. ipv4addrmask = to_bitfield_ipv6('::ffff:0:0')[:96]
  98.  
  99. class IP_List:
  100.     
  101.     def __init__(self):
  102.         self.ipv4list = []
  103.         self.ipv6list = []
  104.  
  105.     
  106.     def __nonzero__(self):
  107.         if not self.ipv4list:
  108.             pass
  109.         return bool(self.ipv6list)
  110.  
  111.     
  112.     def append(self, ip, depth = 256):
  113.         if ip.find(':') < 0:
  114.             insort(self.ipv4list, to_bitfield_ipv4(ip)[:depth])
  115.         else:
  116.             b = to_bitfield_ipv6(ip)
  117.             if b.startswith(ipv4addrmask):
  118.                 insort(self.ipv4list, b[96:][:depth - 96])
  119.             else:
  120.                 insort(self.ipv6list, b[:depth])
  121.  
  122.     
  123.     def includes(self, ip):
  124.         if not self.ipv4list or self.ipv6list:
  125.             return False
  126.         
  127.         if ip.find(':') < 0:
  128.             b = to_bitfield_ipv4(ip)
  129.         else:
  130.             b = to_bitfield_ipv6(ip)
  131.             if b.startswith(ipv4addrmask):
  132.                 b = b[96:]
  133.             
  134.         if len(b) > 32:
  135.             l = self.ipv6list
  136.         else:
  137.             l = self.ipv4list
  138.         for map in l[bisect(l, b) - 1:]:
  139.             if b.startswith(map):
  140.                 return True
  141.             
  142.             if map > b:
  143.                 return False
  144.                 continue
  145.         
  146.         return False
  147.  
  148.     
  149.     def read_fieldlist(self, file):
  150.         
  151.         try:
  152.             f = open(file, 'r')
  153.             lines = f.readlines()
  154.             f.close()
  155.         except:
  156.             print '*** ERROR *** could not read IP range file'
  157.             return None
  158.  
  159.         for line in lines:
  160.             line = line.strip().expandtabs()
  161.             if not line or line[0] == '#':
  162.                 continue
  163.             
  164.             
  165.             try:
  166.                 (line, garbage) = line.split(' ', 1)
  167.             except:
  168.                 pass
  169.  
  170.             
  171.             try:
  172.                 (line, garbage) = line.split('#', 1)
  173.             except:
  174.                 pass
  175.  
  176.             
  177.             try:
  178.                 (ip, depth) = line.split('/')
  179.             except:
  180.                 ip = line
  181.                 depth = None
  182.  
  183.             
  184.             try:
  185.                 if depth is not None:
  186.                     depth = int(depth)
  187.                 
  188.                 self.append(ip, depth)
  189.             continue
  190.             print '*** WARNING *** could not parse IP range: ' + line
  191.             continue
  192.  
  193.         
  194.  
  195.     
  196.     def set_intranet_addresses(self):
  197.         self.append('127.0.0.1', 8)
  198.         self.append('10.0.0.0', 8)
  199.         self.append('172.16.0.0', 12)
  200.         self.append('192.168.0.0', 16)
  201.         self.append('169.254.0.0', 16)
  202.         self.append('::1')
  203.         self.append('fe80::', 16)
  204.         self.append('fec0::', 16)
  205.  
  206.     
  207.     def set_ipv4_addresses(self):
  208.         self.append('::ffff:0:0', 96)
  209.  
  210.  
  211.  
  212. def to_ipv4(ip):
  213.     if ip.find(':') < 0:
  214.         raise ValueError, 'not an IPv6 address'
  215.     
  216.     ip = to_bitfield_ipv6(ip)
  217.     if not ip.startswith(ipv4addrmask):
  218.         raise ValueError, 'not convertible to IPv4'
  219.     
  220.     ip = ip[-32:]
  221.     x = ''
  222.     for i in range(4):
  223.         x += str(int(ip[:8], 2))
  224.         if i < 3:
  225.             x += '.'
  226.         
  227.         ip = ip[8:]
  228.     
  229.     return x
  230.  
  231.  
  232. def is_ipv4(ip):
  233.     return ip.find(':') < 0
  234.  
  235.  
  236. def is_valid_ip(ip):
  237.     
  238.     try:
  239.         if is_ipv4(ip):
  240.             a = ip.split('.')
  241.             if not len(a) == 4:
  242.                 raise AssertionError
  243.             for i in a:
  244.                 chr(int(i))
  245.             
  246.             return True
  247.         
  248.         to_bitfield_ipv6(ip)
  249.         return True
  250.     except:
  251.         return False
  252.  
  253.  
  254.